package com.yh.controller;

import com.yh.config.RestTemplateConfig;
import com.yh.entities.PayDTO;
import com.yh.resp.ResultData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
@RequestMapping("order")
public class OrderController {
    //服务注册中心上的微服务名称
    /*
    由于consul自带负载均衡，会自动轮询调用cloud-payment-service下的微服务，
    因为使用了RestTemplate，所以需要在相关配置类中加上@LoadBalanced注解，
    否则会报错：java.net.UnknownHostException: cloud-payment-service
     */
    public static final String PaymentSrv_URL = "http://cloud-payment-service";

    @Resource
    RestTemplate restTemplate;

    @GetMapping("add")
    public ResultData addOrder(PayDTO payDTO) {
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
    }

    @GetMapping("get/{id}")
    public ResultData getOrderById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/find/"+id, ResultData.class, id);
    }





}
